
package lib.jf;

import java.util.List;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.gsm.SmsManager;
import android.widget.Toast;

public class SMSSender {

    @SuppressWarnings("deprecation")
    public static void sendSMS(final Context context, String adress,String content, final SMSCallBack callback) {
        String SENT_SMS_ACTION = "SENT_SMS_ACTION";
        Intent sentIntent = new Intent(SENT_SMS_ACTION);
        PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent, 0);
        // register the Broadcast Receivers
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context _context, Intent _intent) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        break;
                }
            }
        }, new IntentFilter(SENT_SMS_ACTION));

        // /////////////////////////////////////////////////////////////
        String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
        // create the deilverIntent parameter
        Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
        PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0, deliverIntent, 0);
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context _context, Intent _intent) {
                Toast.makeText(context, "收信人已经成功接收", Toast.LENGTH_SHORT).show();
                if (callback!=null) {
                    callback.success();
                }
            }
        }, new IntentFilter(DELIVERED_SMS_ACTION));

        // /////////////////////////////////////////////////////////////
        // 直接调用短信接口发短信
        SmsManager smsManager = SmsManager.getDefault();
        List<String> divideContents = smsManager.divideMessage(content);
        for (String text : divideContents) {
            smsManager.sendTextMessage(adress, null, text, sentPI, deliverPI);
//            smsManager.sendTextMessage("10001", null, text, sentPI, deliverPI);
        }
    }

//    public void sendSuc(final Context context) {
//        String SENT_SMS_ACTION = "SENT_SMS_ACTION";
//        Intent sentIntent = new Intent(SENT_SMS_ACTION);
//        PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent, 0);
//        // register the Broadcast Receivers
//        context.registerReceiver(new BroadcastReceiver() {
//            @Override
//            public void onReceive(Context _context, Intent _intent) {
//                switch (getResultCode()) {
//                    case Activity.RESULT_OK:
//                        Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show();
//                        break;
//                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
//                        break;
//                    case SmsManager.RESULT_ERROR_RADIO_OFF:
//                        break;
//                    case SmsManager.RESULT_ERROR_NULL_PDU:
//                        break;
//                }
//            }
//        }, new IntentFilter(SENT_SMS_ACTION));
//    }

//    public void acceptSuc(final Context context) {
//        String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
//        // create the deilverIntent parameter
//        Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
//        PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0, deliverIntent, 0);
//        context.registerReceiver(new BroadcastReceiver() {
//            @Override
//            public void onReceive(Context _context, Intent _intent) {
//                Toast.makeText(context, "收信人已经成功接收", Toast.LENGTH_SHORT).show();
//            }
//        }, new IntentFilter(DELIVERED_SMS_ACTION));
//    }

    public interface SMSCallBack {
        public void success();
    }

}
